<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <#if !readOnly><a-button type="primary" @click="onAdd">新增</a-button></#if>
    </a-space>
  </p>
  <!--表格-->
  <a-table :dataSource="${domain}s"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <#if !readOnly>
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
        </#if>
      </template>
      <#list fieldList as field>
        <#if field.enums>
      <template v-else-if="column.dataIndex === '${field.nameHump}'">
        <span v-for="item in ${field.enumsConst}_ARRAY" :key="item.code">
          <span v-if="item.code === record.${field.nameHump}">
            {{item.desc}}
          </span>
        </span>
      </template>
        </#if>
      </#list>
    </template>
  </a-table>
  <#if !readOnly>
  <!--添加表单-->
  <a-modal v-model:visible="visible" title="${tableNameCn}" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="${domain}" :label-col="{span: 5}" :wrapper-col="{ span: 20 }">
      <#list fieldList as field>
        <#if field.name!="id" && field.nameHump!="createTime" && field.nameHump!="updateTime">
      <a-form-item label="${field.nameCn}">
        <#if field.enums>
        <a-select v-model:value="${domain}.${field.nameHump}">
          <a-select-option v-for="item in ${field.enumsConst}_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
        <#elseif field.javaType=='Date'>
          <#if field.type=='time'>
            <a-time-picker v-model:value="${domain}.${field.nameHump}" format="HH:mm" valueFormat="HH:mm"
                           placeholder="请选择时间"/>
          <#elseif field.type=='date'>
        <a-date-picker v-model:value="${domain}.${field.nameHump}" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
          <#else>
            <a-date-picker v-model:value="${domain}.${field.nameHump}" valueFormat="YYYY-MM-DD HH:mm" show-time
                           placeholder="请选择日期"/>
          </#if>
        <#else>
        <a-input v-model:value="${domain}.${field.nameHump}" />
        </#if>
      </a-form-item>
        </#if>
      </#list>
    </a-form>
  </a-modal>
  </#if>
</template>

<script setup>
  import {onMounted, ref} from 'vue';
  import request from "@/utils/request";
  import {notification} from 'ant-design-vue'

  <#list fieldList as field>
<#if field.enums>
// ${tableNameCn}枚举
const ${field.enumsConst}_ARRAY = window.${field.enumsConst}_ARRAY;
</#if>
</#list>
// 新增按钮隐藏属性
const visible = ref(false);
// 表格加载
let loading = ref(false);
// 添加${tableNameCn}的属性
let ${domain} = ref({
  <#list fieldList as field>
  ${field.nameHump}: undefined,
  </#list>
});
// 表格数据
const ${domain}s = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  position: ['bottomCenter'],
  total: 0,
  current: 1,
  pageSize: 5,
  showSizeChanger: true,
  pageSizeOptions: ['5', '10', '20']
});
// 表格展示列
const columns = [
<#list fieldList as field>
  <#if field.name!="id" && field.nameHump!="createTime" && field.nameHump!="updateTime">
  {
    title: '${field.nameCn}',
    dataIndex: '${field.nameHump}',
    key: '${field.nameHump}',
  },
  </#if>
</#list>
<#if !readOnly>
  {
    title: '操作',
    dataIndex: 'operation'
  }
</#if>
];

<#if !readOnly>
// 点击添加按钮，表单显示
const onAdd = () => {
  ${domain}.value = {};
  visible.value = true;
};

// 编辑${tableNameCn}
const onEdit = (record) => {
  ${domain}.value = window.Tool.copy(record);
  visible.value = true;
};

// 删除${tableNameCn}
const onDelete = (record) => {
  request.delete(`/${module}/admin/${do_main}/delete/${r'${record.id}'}`)
    .then((result) => {
      if (result.status) {
        notification.success({description: result.message});
        handleQuery({
          pageNum: pagination.value.current,
          pageSize: pagination.value.pageSize,
        });
      } else {
        notification.error({description: result.message});
      }
  });
};

// 请求 -- 保存${tableNameCn}
const handleOk = () => {
  request.post("/${module}/admin/${do_main}/save", ${domain}.value)
    .then((result) => {
      if (result.status) {
      notification.success({description: result.message});
      visible.value = false;
      handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize
      });
    } else {
      notification.error({description: result.message});
    }
  });
};
</#if>

// 请求 -- 查询${tableNameCn}
const handleQuery = (param) => {
  // 如果无参，则查询当前页
  if (!param) {
    param = {
      pageNum: pagination.value.current,
      pageSize: pagination.value.pageSize
    }
  }
  loading.value = true;
  request.get("/${module}/admin/${do_main}/list", {
    params: {
      pageNum: param.pageNum,
      pageSize: param.pageSize
    }
  }).then((result) => {
    loading.value = false;
    if (result.status) {
      ${domain}s.value = result.content.records;
      // 设置分页控件的值
      pagination.value.current = param.pageNum;
      pagination.value.total = result.content.total;
    } else {
      notification.error({description: result.message});
    }
  });
};

// 表格换页，调用请求
const handleTableChange = (page) => {
  pagination.value = page
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize,
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
});
</script>
